package com.example.user.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

    /*
    * 将泛型指定为 RecyclerView.Adapter.ViewHolder。其中ViewHolder是在RecyclerViewAdapter中定义的一个内部类
    * */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    private List<Imagetext> mImagetextList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;

        public ViewHolder(View view){
            super(view);
            imageView=(ImageView)view.findViewById ( R.id.image );
            textView=(TextView)view.findViewById ( R.id.text );
        }
    }

    public RecyclerViewAdapter(List<Imagetext>imagetextList){
        mImagetextList=imagetextList;
    }

    //创建ViewHolder实例，被LayoutManager所调用
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view=LayoutInflater.from ( parent.getContext () )
                .inflate ( R.layout.recycler_item,parent,false );
        ViewHolder holder=new ViewHolder ( view );
        return holder;
    }
    //用于对RecyclerView子项的数据进行赋值
    public void onBindViewHolder(ViewHolder holder,int position){
        Imagetext imagetext=mImagetextList.get ( position );
        holder.imageView.setImageResource ( imagetext.getImageId () );
        holder.textView.setText ( imagetext.getText () );
    }
    //用于告诉RecyclerView一共有多少子项
    public int getItemCount(){
        return mImagetextList.size ();
    }
}
